package com.sun.dailyprj.view;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.ContentLoadingProgressBar;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.sun.dailyprj.R;
import com.sun.dailyprj.util.Logger;
import com.sun.dailyprj.view.canvas.BesselActivity;
import com.sun.dailyprj.view.canvas.CanvasActivity;


public class CustomViewActivity extends AppCompatActivity implements NonLineProgressRules.CountDownTimerCallback{

    private ContentLoadingProgressBar mProgress;
    private NonLineProgressRules mNonLineRules;
    private TextView mTxtPg;
    private int mPoninter = 0;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_view_main);
        mProgress = findViewById(R.id.toast_ins_pg);
        mTxtPg = findViewById(R.id.txt_ins_pg);
        mProgress.setMax(100);
        mProgress.setMin(0);
//        mNonLineRules = new NonLineProgressRules(2,3*60*1000,100,0,this);
        mHandler.sendEmptyMessage(0);
//        testProgress();
    }

    public void paintView(View view) {
        startActivity(new Intent(this ,CanvasActivity.class));
    }

    public void paintBessel(View view) {

        startActivity(new Intent(this,BesselActivity.class));
    }

   private Handler mHandler = new Handler(new Handler.Callback(){
       @Override
       public boolean handleMessage(Message msg) {
            switch(msg.what){
                case 0:
                    mProgress.setProgress(mPoninter);
                    mTxtPg.setText("进度：" + 0);
                    break;
                case 1:
                    Logger.debug("mPoninter:" + mPoninter);
                    int pg = mNonLineRules.calProgress(mPoninter);
                    Logger.info("pg:%d",pg);
                    mTxtPg.setText("进度：" + pg);
                    mProgress.setProgress(pg);
                    break;
                case 2:
                    Logger.debug("what=2");
                    mTxtPg.setText("超时");
                    break;
            }
           return true;
       }
   });

    private void testProgress(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                for(int i = 0;i<3000;i++){
                    try{
                        Message msg = new Message();
                        msg.what = 1;
                        msg.arg1 = 0;
                        mHandler.sendMessage(msg);
                        Thread.sleep(3*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public void changeProgress(View view) {
        mPoninter ++;
        Logger.debug("changeProgress() mPoninter:" + mPoninter);
    }

    @Override
    public void onFinished() {
        Logger.debug("onFinished()");
        mHandler.sendEmptyMessage(2);
        mNonLineRules.celCountDownTimer();
    }
}
